package com.gastronomy.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gastronomy.constant.Constant;
import com.gastronomy.entity.DishesEntity;
import com.gastronomy.entity.InformationEntity;
import com.gastronomy.entity.User;
import com.gastronomy.service.DishesService;
import com.gastronomy.service.InformationService;
import com.gastronomy.service.UserService;
import com.gastronomy.util.SpringUtil;

@Controller
@RequestMapping("/list")
public class ListController {
	//该 controller 里的方法都是供所有用户查询，无需登录，没有配拦截器
	@Resource
	private DishesService dishesService;
	@Resource
	private UserService userService;
	
	//美食
	@RequestMapping("/dishesIndex")
	public String dishesIndex(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<DishesEntity> foodList =  dishesService.sortDishes("B","createTime","","1",8);
		model.addAttribute("dishesEntities",foodList);
		return "list/dishesIndex";
	}
	
	/**
	 * 此方法用于美食：查询、排序、加载更多操作
	 * 参数：
	 * 1.type:
	 * type==A，查询
	 * type==B，
	 * type==C，加载更多
	 * 
	 * 2.排序类型:sortType
	 * sortType==focuse：按访问量排序，sortType==price：按价格排序，sortType==createTime：按发布时间排序(初始化时默认按发布时间排序)
	 *
	 * 3.foodName：查询操作传过来的参数，其他操作默认为空字符串
	 * 
	 * 4.currentPage：默认为1，每点击一次加载更多，值+1。该值保存在页面的隐藏 input(id=currentPage) 里
	 * 
	 * 5.pageSize：每页显示多少条记录
	 */
	
	@RequestMapping("/listDishes")
	@ResponseBody
	public List<DishesEntity> listDishes(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String type = request.getParameter("type");
		String sortType = request.getParameter("sortType");
		String foodName = request.getParameter("foodName");
		String currentPage = request.getParameter("currentPage");
		List<DishesEntity> foodList =  dishesService.sortDishes(type,sortType,foodName,currentPage,8);
		return foodList;
	}

	
	//商家首页
	@RequestMapping("/businessIndex")
	public String businessIndex(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<User> users =  userService.sortBusiness("B","createTime","","1",8);
		model.addAttribute("userEntities",users);
		return "list/businessIndex";
	}
	/**
	 * 此方法用于美食：查询、排序、加载更多操作
	 * 参数：
	 * 1.type:
	 * type==A，查询
	 * type==B，排序
	 * type==C，加载更多
	 * 
	 * 2.排序类型:nickName(根据名称降序)、createTime(根据注册时间降序)
	 * 
	 * 3.nickName：查询操作传过来的参数，其他操作默认为空字符串
	 * 
	 * 4.currentPage：默认为1，每点击一次加载更多，值+1。该值保存在页面的隐藏 input(id=currentPage) 里
	 */
	
	@RequestMapping("/listBusiness")
	@ResponseBody
	public List<User> listBusiness(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String type = request.getParameter("type");
		String sortType = request.getParameter("sortType");
		String nickName = request.getParameter("nickName");
		String currentPage = request.getParameter("currentPage");
		List<User> userList =  userService.sortBusiness(type,sortType,nickName,currentPage,8);
		return userList;
	}
	
	//查询指定商户下所有美食
		@RequestMapping("/businessDetail")
		public String businessDetail(HttpServletRequest request,
				HttpServletResponse response, Model model) {
			String userId = request.getParameter("userId");
			if(userId==null){
				model.addAttribute("errorMsg","没有此商铺信息");
				return "/loginUser/error";
			}
			User user = userService.queryUserById(userId);
			model.addAttribute("userEntity",user);
			
			List<DishesEntity> result = dishesService.listAllDishesByUserId(Integer.parseInt(userId));
			model.addAttribute("dishesEntities",result);
			return "/list/businessDetail";
		}
}
